// 自定义看板娘加载脚本

// 设置全局配置
window.live2d_settings = {
  // 基本设置
  modelAPI: 'https://api.fghrsh.net/live2d/',
  tipsMessage: '欢迎来到二次元世界~',
  hitokotoAPI: 'https://v1.hitokoto.cn',

  // 设置御姐风格的模型 (6号模型，2号材质)
  modelId: 6,
  modelTexturesId: 2,

  // 允许拖动
  waifuDraggable: 'unlimited',
  waifuDraggableRevert: false,

  // 工具栏设置
  showToolMenu: true,
  canCloseLive2d: false,
  canSwitchModel: true,
  canSwitchTextures: true,
  canSwitchHitokoto: true,
  canTakeScreenshot: true,

  // 提示消息设置
  showHitokoto: true,
  showF12Status: true,
  showCopyMessage: true,

  // 样式设置
  waifuSize: '320x460',
  waifuTipsSize: '250x80',
  waifuFontSize: '15px',
  waifuToolFont: '16px',
  waifuToolLine: '22px',
  waifuToolTop: '0px',
  waifuMinWidth: '768px',

  // 自定义提示语
  waifu: {
    console_open_msg: ['哎呀，你打开了控制台，是想要看到我的小秘密吗？'],
    copy_message: ['你都复制了些什么呀，转载要记得加上出处哦'],
    screenshot_message: ['照好了吗，是不是很可爱呢？'],
    hidden_message: ['我还在这里，等你回来...'],
    load_failed_message: ['模型加载失败，请刷新页面'],
    welcome_message: ['欢迎来到二次元世界~'],
  },
}

// 加载必要的CSS
const loadCSS = () => {
  const link = document.createElement('link')
  link.rel = 'stylesheet'
  link.type = 'text/css'
  link.href =
    'https://cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/waifu.css'
  document.head.appendChild(link)
}

// 创建DOM结构
const createDOM = () => {
  const container = document.createElement('div')
  container.id = 'live2d-widget'
  container.className = 'live2d-widget-container'
  container.style.position = 'fixed'
  container.style.right = '20px'
  container.style.bottom = '0'
  container.style.width = '320px'
  container.style.height = '460px'
  container.style.zIndex = '10000'
  container.style.pointerEvents = 'none'
  container.style.display = 'block'
  container.style.opacity = '1'
  container.style.visibility = 'visible'

  document.body.appendChild(container)

  // 添加内部结构
  container.innerHTML = `
    <div class="waifu">
      <div class="waifu-tips" style="opacity:1;"></div>
      <canvas id="live2d" width="320" height="460" style="position:relative;opacity:1;"></canvas>
      <div class="waifu-tool">
        <span class="fa fa-commenting"></span>
        <span class="fa fa-user-circle"></span>
        <span class="fa fa-street-view"></span>
        <span class="fa fa-camera"></span>
        <span class="fa fa-info-circle"></span>
      </div>
    </div>
  `
}

// 加载主脚本
const loadScript = () => {
  const urls = [
    'https://cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/waifu-tips.js',
    'https://cdn.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/live2d.min.js',
  ]

  urls.forEach((url) => {
    const script = document.createElement('script')
    script.src = url
    document.body.appendChild(script)
  })

  // 确保脚本加载后初始化
  setTimeout(() => {
    if (window.loadlive2d) {
      window.loadlive2d(
        'live2d',
        `${window.live2d_settings.modelAPI}get/?id=${window.live2d_settings.modelId}-${window.live2d_settings.modelTexturesId}`,
      )
    } else {
      console.error('Failed to load live2d scripts')
    }
  }, 2000)
}

// 添加拖动功能
const setupDraggable = () => {
  setTimeout(() => {
    const widget = document.getElementById('live2d-widget')
    if (widget) {
      widget.style.pointerEvents = 'auto'
      widget.style.cursor = 'move'

      let isDragging = false
      let offsetX = 0
      let offsetY = 0

      widget.addEventListener('mousedown', (e) => {
        isDragging = true
        offsetX = e.clientX - widget.getBoundingClientRect().left
        offsetY = e.clientY - widget.getBoundingClientRect().top
        widget.style.transition = 'none'
      })

      document.addEventListener('mousemove', (e) => {
        if (isDragging) {
          widget.style.left = e.clientX - offsetX + 'px'
          widget.style.right = 'auto'
          widget.style.top = e.clientY - offsetY + 'px'
          widget.style.bottom = 'auto'
        }
      })

      document.addEventListener('mouseup', () => {
        isDragging = false
        widget.style.transition = 'all 0.3s ease'
      })
    }
  }, 3000)
}

// 启动函数
const startLive2D = () => {
  loadCSS()
  createDOM()
  loadScript()
  setupDraggable()
}

// 导出函数供其他模块使用
window.startLive2D = startLive2D

// 如果直接加载此脚本，自动启动
if (typeof module === 'undefined') {
  window.addEventListener('DOMContentLoaded', startLive2D)
}

export default startLive2D
